#include <iostream>
#include <cstring>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

using namespace std;

int main() {
    int n = mkfifo("fifo", 0666);
    if (n < 0) {
        cerr << "errno: " << errno << ", errstring:" << strerror(errno) << endl;
        return -1;
    }
    cout << "make fifo sucess……" << endl;

    int rfd = open("fifo", O_RDONLY);
    if (rfd < 0) {
        cerr << "errno: " << errno << ", errstring:" << strerror(errno) << endl;
        return -1;
    }
    char buffer[1024];
    while (true) {
        ssize_t n = read(rfd, buffer, sizeof(buffer) - 1);
        if (n > 0) {
            buffer[n] = '\0';
            cout << "client say:" << buffer << endl;
        } else if (n == 0) {
            cout << "client close me too……" << endl;
            break;
        }
    }
    close(rfd);

    return 0;
}